import os
import time
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "donvan_site.settings")
from blog.models import *

from django.template.defaultfilters import unordered_list
import re

p = Category.objects.all()
print [[i.id, i.parent, i.content] for i in p]


def generate_unordered_list(query_set):
    query_set = [i for i in query_set]
    result = [None]
    parent_idx_offset = 1
    while len(query_set) > 0:
        print result
        parent = result[-parent_idx_offset]
        for i in xrange(len(query_set)):
            if query_set[i].parent == parent:
                result.append(query_set.pop(i))
                parent_idx_offset = 1
                break
        else:
            parent_idx_offset += 1
    result.pop(0)
    return result


print generate_unordered_list(p)